//
//  MyDataPikerView.m
//  MenuAlliance
//
//  Created by Jeoking on 15/9/25.
//  Copyright (c) 2015年 JeoKing. All rights reserved.
//

#import "ZallCustomerDialogView.h"

#define kW [UIScreen mainScreen].bounds.size.width
#define kH [UIScreen mainScreen].bounds.size.height

@interface ZallCustomerDialogView ()<UIGestureRecognizerDelegate>
{
    UIWindow *_window;
    UIView *_customView;
    ShowDirectionType _showDirectionType;
    CGRect _viewShowFrame;   //自定义view显示位置frame
}

@end

@implementation ZallCustomerDialogView

- (instancetype)initWithView:(UIView *)myView
{
    self = [super init];
    if (self) {
        _customView = myView;
        //设置显示的初始位置
        _viewShowFrame = myView.frame;
        _enableTouchHide = YES;
        _isTranslucent = YES;
    }
    return self;
}

- (void)showDialogView:(ShowDirectionType)showDirectionType {
    if (!_window) {
        _showDirectionType = showDirectionType;
        _window = [[UIWindow alloc] init];
        _window.windowLevel = UIWindowLevelAlert;
        UIColor *bgColor;
        //是否背景半透明
        if (_isTranslucent) {
            bgColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5];
        } else {
            bgColor = [UIColor clearColor];
        }
        _window.backgroundColor = bgColor;
        _window.hidden = NO;
        
        //展示方向
        switch (showDirectionType) {
            case ShowFromCenter:
            {
                self.frame = _viewShowFrame;
                self.center = CGPointMake(_window.bounds.size.width/2, _window.bounds.size.height/2);
                self.alpha = 0;
            }
                break;
                
            case ShowFromTop:
                self.frame = CGRectMake(_viewShowFrame.origin.x, -_viewShowFrame.size.height, _viewShowFrame.size.width, _viewShowFrame.size.height);
                break;
                
            case ShowFromBottom:
                self.frame = CGRectMake(_viewShowFrame.origin.x, kH, _viewShowFrame.size.width, _viewShowFrame.size.height);
                break;
                
            case ShowFromLeft:
                self.frame = CGRectMake(-_viewShowFrame.size.width, _viewShowFrame.origin.y, _viewShowFrame.size.width, _viewShowFrame.size.height);
                break;
                
            case ShowFromRight:
                self.frame = CGRectMake(kW, _viewShowFrame.origin.y, _viewShowFrame.size.width, _viewShowFrame.size.height);
                break;
            default:
                break;
        }
        [_window addSubview:self];
        
        _customView.frame = CGRectMake(0, 0, _customView.frame.size.width, _customView.frame.size.height);
        [self addSubview:_customView];
        
        UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(gestureTapEvent:)];
        [tapGesture setNumberOfTapsRequired:1];
        tapGesture.delegate = self;
        [_window addGestureRecognizer:tapGesture];
        
        [UIView animateWithDuration:0.2 animations:^{
            if (showDirectionType == ShowFromCenter) {
                self.alpha = 1;
            } else {
                self.frame = _viewShowFrame;
            }
        }];
    }
}

- (void)dismissDialogView {
    [UIView animateWithDuration:0.3 animations:^{
        switch (_showDirectionType) {
            case ShowFromCenter:
                self.alpha = 0;
                break;
                
            case ShowFromTop:
                self.frame = CGRectMake(_viewShowFrame.origin.x, -_viewShowFrame.size.height, _viewShowFrame.size.width, _viewShowFrame.size.height);
                break;
                
            case ShowFromBottom:
                self.frame = CGRectMake(_viewShowFrame.origin.x, kH, _viewShowFrame.size.width, _viewShowFrame.size.height);
                break;
                
            case ShowFromLeft:
                self.frame = CGRectMake(-_viewShowFrame.size.width, _viewShowFrame.origin.y, _viewShowFrame.size.width, _viewShowFrame.size.height);
                break;
                
            case ShowFromRight:
                self.frame = CGRectMake(kW, _viewShowFrame.origin.y, _viewShowFrame.size.width, _viewShowFrame.size.height);
                break;
            default:
                break;
        }
    } completion:^(BOOL finished) {
        [_customView removeFromSuperview];
        _customView = nil;
        [_window resignKeyWindow];
        [_window removeFromSuperview];
        _window = nil;
    }];
}

-(void)gestureTapEvent:(UITapGestureRecognizer *)gesture {
    if (_enableTouchHide) {
        [self dismissDialogView];
    }
}

#pragma mark - UIGestureRecognizerDelegate
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
    // 若为UIWindow，则触发，否则截获Touch事件
    if ([touch.view isKindOfClass:[UIWindow class]]) {
        return YES;
    }
    return  NO;
}

@end
